<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');


$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
if($_RQ['op']=='main'){
	
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	//查询进入该页面的客户所有订单
	$params=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status']) && $_RQ['status']!=100){
		$params['status']=intval($_RQ['status']);
		$return['status']=intval($_RQ['status']);
	}else{
		$return['status']=100;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_meye_server_order',$params,array($pindex,$psize),$total,array(),'','id desc');
	if($list){
		foreach($list as &$item){
			$member=model('core/member')->detail($item['mid'],array('nickname','mobile'));
			$serve=pdo_get('jy_meye_serve',array('id'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['store']=pdo_get('jy_meye_store',array('id'=>$item['store_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['user_name']=$member['nickname'];
			$item['user_mobile']=$member['mobile'];
			$item['serve_title']=$serve['title'];
			$item['serve_image']=$serve['image'];
			$item['package']=json_decode($item['package']);
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$item['serve_typetitle']=$this->serve_types[$item['serve_type']];
			$serve_times=json_decode($serve['serve_time'],true);
			$item['serve_time']=$serve_times[$item['timeid']];
			
		}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_meye_server_order',array('status'=>'4'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//确认订单后，计算员工佣金,存入佣金日志表，并更新员工表员工总佣金
	$worker_id=pdo_getcolumn('jy_meye_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'worker_id');
	$pay=pdo_getcolumn('jy_meye_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'pay');
	$lev_id=pdo_getcolumn('jy_meye_store_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id),'lev');
	$rate=pdo_getcolumn('jy_meye_lev',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lev_id),'rate');
	if($rate){
		$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'store_id'=>pdo_getcolumn('jy_meye_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'store_id'),
		'worker_id'=>$worker_id,
		'order_id'=>$_RQ['id'],
		'money'=>$pay*$rate/100,
		'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_meye_commission_log',$insert);
		$commission=pdo_getcolumn('jy_meye_store_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id),'commission');
		$commission=$commission+$pay*$rate/100;
		pdo_update('jy_meye_store_worker',array('commission'=>$commission),array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id));
	}
	
	exi('确认服务成功');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_meye_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除预约成功');
}
if($_RQ['op']=='pay'){
	$post=get_postdata();
	$id=$post['id'];
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$pay_id=pdo_getcolumn('jy_meye_server_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'pay_id');
	exi($pay_id);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_meye_server_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_meye_server_order',array('status'=>1,'pay'=>$payresult['fee']),array('id'=>$order['id']));
	exi('支付成功','success','jy_meye/server_order/list/status=1');
}